/*
 * @Author: linzeqin
 * @Date: 2019-09-29 14:33:19
 * @description: jenkins任务列表
 */
import { Model, Document, Schema } from '.';
import { Application } from 'egg';

/** 单个工程构建需要的参数 */
export interface JenkinsPlanJobItem {
    /** 项目名称 */
    name: string;
    /** 描述 */
    description?: string;
    /** 构建参数 */
    params: object;
    /** 状态 */
    status?: number;
    /** 执行结果 */
    result?: object;
    /** jenkins路径 */
    jenkinsUrl?: string;
    /** task名称 */
    taskName?: string;
    /** task序号 */
    taskNo?: number;
    /** taskid */
    taskId?: string;
}

/** jenkins计划实体 */
export class JenkinsPlanReport {
    /** 名称 */
    name: string = '';

    /** 描述 */
    description?: string = '';

    /** 状态，1启用，2禁用 */
    status?: number = 1;

    /** 创建人uid */
    creatorUid: string = '';

    /** 计划构建的项目 */
    jobList: JenkinsPlanJobItem[] = [];

    /** 执行时间 */
    executionTime: number = new Date().getTime();

    /** 创建时间 */
    createTime: number = new Date().getTime();

    /** 修改时间 */
    updateTime: number = new Date().getTime();
}

export interface IJenkinsPlanReportModel extends Document, JenkinsPlanReport {}

export default function JenkinsPlanModel(
    app: Application,
): Model<IJenkinsPlanReportModel> {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;
    const jenkinsPlanSchema: Schema<JenkinsPlanReport> = new Schema({
        name: { type: String },
        description: { type: String },
        status: { type: Number, default: 1 },
        creatorUid: { type: String },
        jobList: { type: Array },
        executionTime: { type: Number },
        createTime: { type: Number },
        updateTime: { type: Number },
    }, {
        timestamps: { updatedAt: 'updateTime', createdAt: 'createTime' },
    });
    return mongoose.model<IJenkinsPlanReportModel>(
        'jenkins_plan_report',
        jenkinsPlanSchema,
    );
}
